<?xml version="1.0" encoding="UTF-8"?>

<!--
    Document   : marc2ese.xsl
    Created on : 9. květen 2011, 18:18
    Author     : Martin Řehánek (rehan at mzk.cz)
    Description:
        Purpose of transformation follows.
-->

<xsl:stylesheet version="2.0"
                xmlns:marc="http://www.loc.gov/MARC21/slim"
                xmlns:europeana="http://www.europeana.eu/schemas/ese/"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:dc="http://purl.org/dc/elements/1.1/"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                exclude-result-prefixes="marc"
                xsi:schemaLocation="http://www.w3.org/1999/XSL/Transform http://www.w3.org/2007/schema-for-xslt20.xsd"
>
    <xsl:import href="http://www.loc.gov/standards/marcxml/xslt/MARC21slimUtils.xsl"/>
    <xsl:import href="http://iris.mzk.cz/cache/xslt/functions.xsl"/>
    <!--    <xsl:import href="/home/rehan/NetBeansProjects/repositoryManager/src/cz/mzk/oai/xslt/functions.xsl"/>-->
    <xsl:output method="xml" indent="yes" />
    <!--
                Suppressed duplicate 520,521. fixed 752 subfield list, suppressed
                856q, added 662. 2008-01-22 ntra Fixed 500 fields. 2006-12-11 ntra
                Added ISBN and deleted attributes 6/04 jer
    -->
    <!--    TODO: striping doesn't work-->
    <xsl:strip-space elements="europeana:isShownAt europeana:isShownBy europeana:object"/>
    <xsl:variable name="organization">Moravská zemská knihovna v Brně / Moravian Library</xsl:variable>
    
    <xsl:template name="root">
        <xsl:param name="agregator"/>
        <xsl:param name="type"/>
        <xsl:param name="subject"/>
        <xsl:param name="link_to_image"/>
        <!-- supported values: 856uImageServer, 856uK4Link -->
        <xsl:param name="link_to_page"/>
        <!-- supported values: aleph, 856uK4Link -->
        
        <xsl:if test="marc:collection">
            <europeana:metadata xsi:schemaLocation="http://www.europeana.eu/schemas/ese/ http://www.europeana.eu/schemas/ese/ESE-V3.4.xsd">
                <xsl:for-each select="marc:collection">
                    <xsl:for-each select="marc:record">
                        <europeana:record>
                            <dc:source>
                                <xsl:value-of select="$organization"/>
                            </dc:source>
                            <xsl:call-template name="marc_record">
                                <xsl:with-param name="agregator" select="$agregator"/>
                                <xsl:with-param name="type" select="$type"/>
                                <xsl:with-param name="subject" select="$subject"/>
                                <xsl:with-param name="link_to_image" select="$link_to_image"/>
                                <xsl:with-param name="link_to_page" select="$link_to_page"/>
                            </xsl:call-template> 
                        </europeana:record>
                    </xsl:for-each>
                </xsl:for-each>
            </europeana:metadata>
        </xsl:if>

        <xsl:if test="marc:record">
            <europeana:metadata
                xsi:schemaLocation="http://www.europeana.eu/schemas/ese/ http://www.europeana.eu/schemas/ese/ESE-V3.4.xsd">
                <europeana:record>
                    <dc:source>
                        <xsl:value-of select="$organization"/>
                    </dc:source>
                    <xsl:for-each select="marc:record">
                        <xsl:call-template name="marc_record">
                            <xsl:with-param name="agregator" select="$agregator"/>
                            <xsl:with-param name="type" select="$type"/>
                            <xsl:with-param name="subject" select="$subject"/>
                            <xsl:with-param name="link_to_image" select="$link_to_image"/>
                            <xsl:with-param name="link_to_page" select="$link_to_page"/>
                        </xsl:call-template> 
                    </xsl:for-each>
                </europeana:record>
            </europeana:metadata>
        </xsl:if>
    </xsl:template>
    
    <xsl:template name="marc_record">
        <xsl:param name="agregator"/>
        <xsl:param name="type"/>
        <xsl:param name="subject"/>
        <xsl:param name="link_to_image"/>
        <xsl:param name="link_to_page"/>
        <xsl:variable name="leader" select="marc:leader" />
        <xsl:variable name="leader6" select="substring($leader,7,1)" />
        <xsl:variable name="leader7" select="substring($leader,8,1)" />
        <xsl:variable name="controlField008" select="marc:controlfield[@tag='008']" />
        <dc:identifier>
            <xsl:value-of select="marc:controlfield[@tag='003']"/>:
            <xsl:value-of select="marc:controlfield[@tag='001']"/>
        </dc:identifier>
        <xsl:for-each select="marc:datafield[@tag='245']">
            <dc:title>
                <xsl:variable name="subP" select="./marc:subfield[@code='p']"/>
                <xsl:choose>
                    <xsl:when test="$subP">
                        <xsl:call-template name="removeSquareBrackets">
                            <xsl:with-param name="text">
                                <xsl:call-template name="trimTo100chars">
                                    <xsl:with-param name="text">
                                        <xsl:value-of select="$subP"/>
                                    </xsl:with-param>
                                </xsl:call-template>
                            </xsl:with-param>
                        </xsl:call-template>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:call-template name="removeSquareBrackets">
                            <xsl:with-param name="text">
                                <xsl:call-template name="trimTo100chars">
                                    <xsl:with-param name="text">
                                        <xsl:for-each select="./marc:subfield[@code='a']|./marc:subfield[@code='b']|./marc:subfield[@code='c']">
                                            <xsl:value-of select="."/>
                                        </xsl:for-each>
                                    </xsl:with-param>
                                </xsl:call-template>
                            </xsl:with-param>
                        </xsl:call-template>
                    </xsl:otherwise>
                </xsl:choose>
            </dc:title>
        </xsl:for-each>
        <xsl:for-each
            select="marc:datafield[@tag='100']/marc:subfield[@code='a']|marc:datafield[@tag='110']/marc:subfield[@code='a']|marc:datafield[@tag='111']/marc:subfield[@code='a']|marc:datafield[@tag='700']/marc:subfield[@code='a']|marc:datafield[@tag='710']/marc:subfield[@code='a']|marc:datafield[@tag='711']/marc:subfield[@code='a']|marc:datafield[@tag='720']/marc:subfield[@code='a']">
            <dc:creator>
                <xsl:call-template name="removeEndComma">
                    <xsl:with-param name="text">
                        <xsl:value-of select="."/>
                    </xsl:with-param>
                </xsl:call-template>
            </dc:creator>
        </xsl:for-each>
        <dc:type>
            <xsl:value-of select="$type"/>
        </dc:type>
        <dc:type>
            <xsl:if test="$leader7='c'">
                <!--Remove attribute 6/04 jer-->
                <!--<xsl:attribute name="collection">yes</xsl:attribute>-->
                <xsl:text>collection</xsl:text>
            </xsl:if>
            <xsl:if
                test="$leader6='d' or $leader6='f' or $leader6='p' or $leader6='t'">
                <!--Remove attribute 6/04 jer-->
                <!--<xsl:attribute name="manuscript">yes</xsl:attribute>-->
                <xsl:text>manuscript</xsl:text>
            </xsl:if>
            <xsl:choose>
                <xsl:when test="$leader6='a' or $leader6='t'">
                    text
                </xsl:when>
                <xsl:when test="$leader6='e' or $leader6='f'">
                    cartographic
                </xsl:when>
                <xsl:when test="$leader6='c' or $leader6='d'">
                    notated music
                </xsl:when>
                <xsl:when test="$leader6='i' or $leader6='j'">
                    sound recording
                </xsl:when>
                <xsl:when test="$leader6='k'">
                    still image
                </xsl:when>
                <xsl:when test="$leader6='g'">
                    moving image
                </xsl:when>
                <xsl:when test="$leader6='r'">
                    three dimensional object
                </xsl:when>
                <xsl:when test="$leader6='m'">
                    software, multimedia
                </xsl:when>
                <xsl:when test="$leader6='p'">
                    mixed material
                </xsl:when>
            </xsl:choose>
        </dc:type>
        <xsl:for-each select="marc:datafield[@tag='655']">
            <dc:type>
                <xsl:call-template name="subfieldSelect">
                    <xsl:with-param name="codes">
                        abcdq
                    </xsl:with-param>
                </xsl:call-template>
            </dc:type>
        </xsl:for-each>
        <xsl:for-each select="marc:datafield[@tag='260']">
            <dc:publisher>
                <!--                <xsl:call-template name="removeOpeningSquareBracket">
                <xsl:with-param name="text">-->
                <xsl:value-of select="marc:subfield[@code='a']" />
                <!--                    </xsl:with-param>
                </xsl:call-template>-->
                
                <xsl:variable name="withoutEndComma">
                    <xsl:call-template name="removeEndComma">
                        <xsl:with-param name="text">
                            <xsl:value-of select="marc:subfield[@code='b']"/>
                        </xsl:with-param>
                    </xsl:call-template>
                </xsl:variable>
                
                <xsl:call-template name="addClosingBracketIfMissing">
                    <xsl:with-param name="text">
                        <xsl:value-of select="$withoutEndComma"/>
                    </xsl:with-param>
                </xsl:call-template>
            </dc:publisher>
        </xsl:for-each>
        <xsl:for-each select="marc:datafield[@tag='260']/marc:subfield[@code='c']">
            <!--            TODO: rozsirit pomoci dcterms:created a dcterms:issued-->
            <dc:date>
                <xsl:call-template name="addOpeningSquareBracketIfMissing">
                    <xsl:with-param name="text">
                        <xsl:value-of select="." />
                    </xsl:with-param>
                </xsl:call-template>
            </dc:date>
        </xsl:for-each>
        <xsl:for-each select="marc:datafield[@tag='041']/marc:subfield[@code='a']">
            <dc:language>
                <xsl:value-of select="." />
            </dc:language>
        </xsl:for-each>

        <!--TODO: poresit language. Kvuli chybe v 008 ted nepouziju hodnotu z 008
            Ulozeni jazyku
            primarni hodnota v substring($controlField008,36,3)
            pokud je vic jazyku, je primarni jazyk zopakovan krome 008 i v 041a a potom dalsie
        -->
        <!--        <dc:language>
            <xsl:value-of select="substring($controlField008,36,3)" />
        </dc:language>-->
        <!--
                <xsl:for-each
                select="marc:datafield[@tag=856]/marc:subfield[@code='q']">
                <dc:format> <xsl:value-of select="."/> </dc:format> </xsl:for-each>
        -->
        <!--
                <xsl:for-each select="marc:datafield[@tag=520]"> <dc:description>
                <xsl:value-of select="marc:subfield[@code='a']"/> </dc:description>
                </xsl:for-each>
        -->
        <!--
                <xsl:for-each select="marc:datafield[@tag=521]"> <dc:description>
                <xsl:value-of select="marc:subfield[@code='a']"/> </dc:description>
                </xsl:for-each>
        -->
        <xsl:for-each
            select="marc:datafield[@tag='500' and @tag='599'][not(@tag='506' or @tag='530' or @tag='540' or @tag='546')]">
            <dc:description>
                <xsl:value-of select="."/>
            </dc:description>
        </xsl:for-each>
        <dc:subject>
            <xsl:value-of select="$subject"/>
        </dc:subject>
        <xsl:for-each select="marc:datafield[@tag='600']">
            <dc:subject>
                <xsl:call-template name="subfieldSelect">
                    <xsl:with-param name="codes">
                        a
                    </xsl:with-param>
                </xsl:call-template>
            </dc:subject>
        </xsl:for-each>
        <xsl:for-each select="marc:datafield[@tag='610']">
            <dc:subject>
                <xsl:call-template name="subfieldSelect">
                    <xsl:with-param name="codes">
                        abcdq
                    </xsl:with-param>
                </xsl:call-template>
            </dc:subject>
        </xsl:for-each>
        <xsl:for-each select="marc:datafield[@tag='611']">
            <dc:subject>
                <xsl:call-template name="subfieldSelect">
                    <xsl:with-param name="codes">
                        abcdq
                    </xsl:with-param>
                </xsl:call-template>
            </dc:subject>
        </xsl:for-each>
        <xsl:for-each select="marc:datafield[@tag='630']">
            <dc:subject>
                <xsl:call-template name="subfieldSelect">
                    <xsl:with-param name="codes">
                        abcdq
                    </xsl:with-param>
                </xsl:call-template>
            </dc:subject>
        </xsl:for-each>
        <xsl:for-each select="marc:datafield[@tag='650']">
            <dc:subject>
                <xsl:call-template name="subfieldSelect">
                    <xsl:with-param name="codes">
                        abcdq
                    </xsl:with-param>
                </xsl:call-template>
            </dc:subject>
        </xsl:for-each>
        <xsl:for-each select="marc:datafield[@tag='653']">
            <dc:subject>
                <xsl:call-template name="subfieldSelect">
                    <xsl:with-param name="codes">
                        abcdq
                    </xsl:with-param>
                </xsl:call-template>
            </dc:subject>
        </xsl:for-each>
        <xsl:for-each select="marc:datafield[@tag='662']">
            <dc:coverage>
                <xsl:call-template name="subfieldSelect">
                    <xsl:with-param name="codes">
                        abcdefgh
                    </xsl:with-param>
                </xsl:call-template>
            </dc:coverage>
        </xsl:for-each>
        <xsl:for-each select="marc:datafield[@tag='752']">
            <dc:coverage>
                <xsl:call-template name="subfieldSelect">
                    <xsl:with-param name="codes">
                        adcdfgh
                    </xsl:with-param>
                </xsl:call-template>
            </dc:coverage>
        </xsl:for-each>
        <xsl:for-each select="marc:datafield[@tag='530']">
            <dc:relation type="original">
                <xsl:call-template name="subfieldSelect">
                    <xsl:with-param name="codes">
                        abcdu
                    </xsl:with-param>
                </xsl:call-template>
            </dc:relation>
        </xsl:for-each>
        <xsl:for-each
            select="marc:datafield[@tag='760']|marc:datafield[@tag='762']|marc:datafield[@tag='765']|marc:datafield[@tag='767']|marc:datafield[@tag='770']|marc:datafield[@tag='772']|marc:datafield[@tag='773']|marc:datafield[@tag='774']|marc:datafield[@tag='775']|marc:datafield[@tag='776']|marc:datafield[@tag='777']|marc:datafield[@tag='780']|marc:datafield[@tag='785']|marc:datafield[@tag='786']|marc:datafield[@tag='787']">
            <dc:relation>
                <xsl:call-template name="subfieldSelect">
                    <xsl:with-param name="codes">
                        ot
                    </xsl:with-param>
                </xsl:call-template>
            </dc:relation>
        </xsl:for-each>
        <xsl:for-each select="marc:datafield[@tag='506']">
            <dc:rights>
                <xsl:value-of select="marc:subfield[@code='a']" />
            </dc:rights>
        </xsl:for-each>
        <xsl:for-each select="marc:datafield[@tag='540']">
            <dc:rights>
                <xsl:value-of select="marc:subfield[@code='a']" />
            </dc:rights>
        </xsl:for-each>
        <europeana:object>
            <xsl:call-template name="link_to_picture">
                <xsl:with-param name="link_to_image" select="$link_to_image"/>
            </xsl:call-template>
        </europeana:object>
        <europeana:provider>
            <xsl:value-of select="$agregator"/>
        </europeana:provider>
        <europeana:type>
            <xsl:value-of select="$type"/>
        </europeana:type>
        <europeana:rights>http://www.europeana.eu/rights/rr-f/</europeana:rights>
        <europeana:dataProvider>
            <xsl:value-of select="$organization"/>
        </europeana:dataProvider>
        <!-- pomoci signatury
        <xsl:for-each select="marc:datafield[@tag=910]">
            <europeana:isShownAt>http://aleph.mzk.cz/F/?func=find-c&amp;ccl_term=SIG+%3D+"<xsl:value-of select="marc:subfield[@code='b']"/>"&amp;local_base=MZK03</europeana:isShownAt>
        </xsl:for-each> -->
        <!-- isShownBy se tvori jen z prvniho datafieldu 856 -->
        <europeana:isShownBy>
            <xsl:call-template name="link_to_picture">
                <xsl:with-param name="link_to_image" select="$link_to_image"/>
            </xsl:call-template>
        </europeana:isShownBy>
        <europeana:isShownAt>
            <xsl:choose>
                <xsl:when test="$link_to_page = 'aleph'">
                    <xsl:call-template name="alephLink"/>
                </xsl:when>
                <xsl:when test="$link_to_page = '856uK4Link'">
                    <xsl:call-template name="k4_link_from_856"/>
                </xsl:when>
            </xsl:choose>
        </europeana:isShownAt>
    </xsl:template>
    
    <xsl:template name="link_to_picture">
        <xsl:param name="link_to_image"/>
        <xsl:choose>
            <xsl:when test="$link_to_image = '856uImageServer'">
                <xsl:call-template name="imgServer_big_picture"/>
            </xsl:when>
            <xsl:when test="$link_to_image = '856uK4Link'">
                <xsl:call-template name="k4_picture_from_handle"/>
            </xsl:when>
        </xsl:choose>
    </xsl:template>
    
</xsl:stylesheet>
